package org.bees.cms.core;

import java.io.Serializable;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

import org.bees.cms.core.util.record.Record;
import org.bees.cms.core.util.record.Recordable;
import org.bees.cms.core.util.record.Recorder;

/**
 * 所有实体类的顶级超类,该类提供了 主键,版本,是否删除,备注,添加记录,修改记录属性<br/>
 * 同时该类提供默认注入添加记录和修改记录的功能,所以开发者只需要更加关心业务逻辑即可
 * 
 * @author 杨昌沛
 * 
 */
@MappedSuperclass
@EntityListeners(value = { Recorder.class })
public abstract class Domain extends Dao implements Recordable, Serializable {
	private static final long serialVersionUID = -105817637513752463L;

	protected Long id;
	protected Integer version;
	protected Boolean visible = true;
	protected String remark;
	protected Record create;
	protected Record modify;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Version
	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	@Column(nullable = false)
	public Boolean getVisible() {
		return visible;
	}

	public void setVisible(Boolean visible) {
		this.visible = visible;
	}

	@Column(length = 100)
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@Embedded
	@AttributeOverrides(value = { @AttributeOverride(name = "operator", column = @Column(name = "create_operator", length = 24, nullable = false)), @AttributeOverride(name = "time", column = @Column(name = "create_time", nullable = false)) })
	@Override
	public Record getCreate() {
		return create;
	}

	@Override
	public void setCreate(Record create) {
		this.create = create;
	}

	@Embedded
	@AttributeOverrides(value = { @AttributeOverride(name = "operator", column = @Column(name = "modify_operator", length = 24, nullable = false)), @AttributeOverride(name = "time", column = @Column(name = "modify_time", nullable = false)) })
	@Override
	public Record getModify() {
		return modify;
	}

	@Override
	public void setModify(Record modify) {
		this.modify = modify;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Domain other = (Domain) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

}
